﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using VIBlend.WPF.Controls;

namespace AnonsaiWPF.CSharp.ComboBoxDemos
{
    public partial class ComboBoxOverview : Window, IWindow
    {
        public ComboBoxOverview()
        {
            InitializeComponent();
            this.BindCombo();
        }


        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void checkBox2_Checked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            if (this.checkBox2 == null)
                return;

            this.viblendCombo.ShowOnlyFilteredItems = checkBox2.IsChecked.Value;
        }

        void checkBox2_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            if (this.checkBox2 == null)
                return;

            this.viblendCombo.ShowOnlyFilteredItems = checkBox2.IsChecked.Value;
        }

        private void comboBox2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            if (this.comboBox2 == null)
                return;

            switch (comboBox2.SelectedIndex)
            {
                case 0:
                    this.viblendCombo.FilterMode = EditFilterMode.Contains;
                    break;
                case 1:
                    this.viblendCombo.FilterMode = EditFilterMode.StartsWith;
                    break;
            }
        }

        private void checkBox3_Checked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;
            
            // When the IsEditable property is true, you can type into the combobox edit field. Otherwise the comboBox works as a DropDownList.
            this.viblendCombo.IsEditable = true;
        }

        private void checkBox3_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;
            
            // When the IsEditable property is true, you can type into the combobox edit field.  Otherwise the comboBox works as a DropDownList.
            this.viblendCombo.IsEditable = false;
        }

        private void comboBox4_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;
            
            if (this.comboBox4 == null)
                return;

            VIBlend.WPF.Controls.ComboBoxItem item = comboBox4.SelectedItem as VIBlend.WPF.Controls.ComboBoxItem;
            if (item == null)
                return;

            if (this.comboBox4.SelectedIndex == 0)
            {
                this.viblendCombo.DropDownWidth = Double.NaN;
                return;
            }

            double value = double.Parse(item.Content.ToString());

            this.viblendCombo.DropDownWidth = value;
        }

        private void comboBox6_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;
            
            if (this.comboBox6 == null)
                return;

            VIBlend.WPF.Controls.ComboBoxItem item = comboBox6.SelectedItem as VIBlend.WPF.Controls.ComboBoxItem;
            if (item == null)
                return;

            if (this.comboBox6.SelectedIndex == 0)
            {
                this.viblendCombo.DropDownHeight = Double.NaN;
                return;
            }

            double value = double.Parse(item.Content.ToString());

            this.viblendCombo.DropDownHeight = value;
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;
            
            if (this.comboBox1 == null)
                return;

            // LayoutMode property changes the items layout. 
            switch (comboBox1.SelectedIndex)
            {
                case 0:
                    this.viblendCombo.LayoutMode = LayoutModes.Vertical;
                    break;
                case 1:
                    this.viblendCombo.LayoutMode = LayoutModes.VerticalWrap;
                    break;
                case 2:
                    this.viblendCombo.LayoutMode = LayoutModes.HorizontalWrap;
                    break;
                case 3:
                    this.viblendCombo.LayoutMode = LayoutModes.Columns;
                    break;
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            BindCombo();
        }

        private void BindCombo()
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.ItemsSource = null;
            GenerateSampleData();

            this.viblendCombo.ItemsSource = sampleList;
            this.viblendCombo.SelectedIndex = 5;
        }

        List<string> sampleList;

        private void GenerateSampleData()
        {
            #region SampleData
            string[] firstNames = new string[] 
                {
                    "Andrew",
                    "Nancy",
                    "Shelley",
                    "Regina",
                    "Yoshi",
                    "Antoni",
                    "Mayumi",
                    "Ian",
                    "Peter",
                    "Lars",
                    "Petra",
                    "Martin",
                    "Sven",
                    "Eric",
                    "Elio",
                    "Beate",
                    "Cheryl",
                    "Ivan",
                    "Maria",
                    "Lucas",
                    "Monica",
                    "John",
                    "George",
                    "Mark",
                    "Mohan",
                 };

            string[] lastNames = new string[] 
                {
                    "Fuller",
                    "Davolio",
                    "Burke",
                    "Murphy",
                    "Nagase",
                    "Ohno",
                    "Devling",
                    "Wilson",
                    "Winkler",
                    "Bein",
                    "Rossi",
                    "Vileid",
                    "Saylor",
                    "Björn",
                    "Nodier",
                    "Spears",
                    "Kumar",
                    "Wei",
                    "Nelson",
                    "Duncan",
                    "Bianchi",
                    "Murdock",
                    "Lating",
                    "Sweet",
                };
            #endregion

            sampleList = new List<string>();
            Random rand = new Random();

            for (int i = 0; i < 30; i++)
            {
                string record =
                        firstNames[rand.Next(0, firstNames.Length - 1)] + " " +
                        lastNames[rand.Next(0, lastNames.Length - 1)];

                if (!sampleList.Contains(record))
                    sampleList.Add(record);
            }
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;
            
            this.viblendCombo.Items.Clear();
        }

        private void button3_Click_1(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;
            
            this.viblendCombo.SelectedIndex = -1;
        }

        private void checkBox4_Checked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;
            
            if (this.checkBox4 == null)
                return;

            this.viblendCombo.IsArrowBeforeContent = false;
        }

        private void checkBox4_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.checkBox4 == null)
                return;

            this.viblendCombo.IsArrowBeforeContent = true;
        }

        private void checkBox8_Checked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.IsResizable = true;
        }

        private void checkBox8_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.IsResizable = false;
        }

        private void comboBox5_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.comboBox5 == null)
                return;

            switch (this.comboBox5.SelectedIndex)
            {
                case 0:
                    this.viblendCombo.ArrowBrush = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
                    break;
                case 1:
                    this.viblendCombo.ArrowBrush = new SolidColorBrush(Colors.Blue);
                    break;
                case 2:
                    this.viblendCombo.ArrowBrush = new SolidColorBrush(Colors.Red);
                    break;
                case 3:
                    this.viblendCombo.ArrowBrush = new SolidColorBrush(Color.FromArgb(255, 4, 34, 113));
                    break;
                case 4:
                    this.viblendCombo.ArrowBrush = new SolidColorBrush(Colors.Green);
                    break;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.UndoSelection();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.RedoSelection();
        }

        private void checkBox6_Checked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.IsFilteringEnabled = true;
        }

        private void checkBox6_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.IsFilteringEnabled = false;
        }

        private void checkBox5_Checked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.OpenDropDownOnFocus = true;
        }

        private void checkBox5_Unchecked(object sender, RoutedEventArgs e)
        {
            if (this.viblendCombo == null)
                return;

            this.viblendCombo.OpenDropDownOnFocus = false;
        }

        public void DetachContent()
        {
            RemoveLogicalChild(Content);
        }

        public void SetTheme()
        {
        }

        public Grid Root
        {
            get
            {
                return this.RootVisual;
            }
        } 
    }
}
